package com.cold.legendary.bprocess.db.model.base;

import lombok.Data;

import java.io.Serializable;

/**
 * @author cold
 * @since 2023/3/9
 */
@Data
public class PageParamsDto implements Serializable {
    private static final long serialVersionUID = 4494716472055375042L;
    private static final Integer DEFUALT_PAGE_SIZE = 15;
    public static final Integer DEFUALT_CUR_PAGE = 1;
    private static final Integer DEFAULT_TOTAL_NUM = 0;
    /**
     * 分页中，页面大小，即每页显示多少的数据
     */
    private Integer pageSize = DEFUALT_PAGE_SIZE;
    private Integer curPage = DEFUALT_CUR_PAGE;
    private Integer totalNum = DEFAULT_TOTAL_NUM;
    private Integer startIndex;
    private Integer endIndex;

    public PageParamsDto() {
        calculateIndex();
    }

    public PageParamsDto(Integer curPage, Integer pageSize) {
        this.curPage = curPage;
        this.pageSize = pageSize;
        calculateIndex();
    }

    /**
     * 计算游标信息
     */
    private void calculateIndex() {
        if (this.curPage == null || this.curPage <= 1) {
            this.curPage = DEFUALT_CUR_PAGE;
        }
        if (this.pageSize == null || this.pageSize <= 0) {
            this.pageSize = DEFUALT_PAGE_SIZE;
        }
        this.startIndex = (this.curPage - 1) * this.pageSize;
        this.endIndex = this.curPage * this.pageSize > this.totalNum ? totalNum : this.curPage * this.pageSize;
    }
}
